Language Reference / Context options and parameters

Context options and parameters

PHP offers various context options and parameters which can be used with all filesystem and stream wrappers. The context is created with stream_context_create(). Options are set with stream_context_set_option() and parameters with stream_context_set_params().


Socket context options

Socket context options are available for all wrappers that work over sockets, like tcp, http and ftp.

Options
  1. bindto:
    1. Used to specify the IP address (either IPv4 or IPv6) and/or the port number that PHP will use to access the network. The syntax is ip:port for IPv4 addresses, and [ip]:port for IPv6 addresses. Setting the IP or the port to 0 will let the system choose the IP and/or port.
    2. Note: As FTP creates two socket connections during normal operation, the port number cannot be specified using this option.
  2. backlog
    1. Used to limit the number of outstanding connections in the socket's listen queue
    2. Note: This is only applicable to stream_socket_server().
  3. ipv6_v6only
    1. Overrides the OS default regarding mapping IPv4 into IPv6.
    2. This is important in particular(尤其) when trying to listen on IPv4 addresses separately(分別) while there exists a binding on [::].
    3. This is only applicable to stream_socket_server().
  4. so_reuseport
    1. Allows multiple bindings to a same ip:port pair, even from separate processes.
    2. Note: This is only applicable to stream_socket_server().
  5. so_broadcast
    1. Enables sending and receiving data to/from broadcast addresses.
    2. This is only applicable to stream_socket_server().

Examples: Basic bindto usage example

<?php
// connect to the internet using the '192.168.0.100' IP
$opts = array(
    'socket' => array(
        'bindto' => '192.168.0.100:0',
    ),
);


// connect to the internet using the '192.168.0.100' IP and port '7000'
$opts = array(
    'socket' => array(
        'bindto' => '192.168.0.100:7000',
    ),
);


// connect to the internet using the '2001:db8::1' IPv6 address
// and port '7000'
$opts = array(
    'socket' => array(
        'bindto' => '[2001:db8::1]:7000',
    ),
);


// connect to the internet using port '7000'
$opts = array(
    'socket' => array(
        'bindto' => '0:7000',
    ),
);


// create the context...
$context = stream_context_create($opts);

// ...and use it to fetch the data
echo file_get_contents('http://www.example.com', false, $context);

?>

HTTP context options

Context options for http:// and https:// transports

Options
  1. method
    1. Type: string
    2. GET, POST, or any other HTTP method supported by the remote server
  2. header
    1. Type: array, string
    2. Additional headers to be sent during request. Values in this option will override other values (such as User-agent:, Host:, and Authentication:).
  3. user_agent
    1. Type: string
    2. Value to send with User-Agent: header. This value will only be used if user-agent is not specified in the header context option above
    3. Note: By default the user_agent php.ini setting is used
  4. content
    1. Type: string
    2. Additional data to be sent after the headers. Typically used with POST or PUT requests
  5. proxy
    1. Type: string
    2. URI specifying address of proxy server. (e.g. tcp://proxy.example.com:5100).
  6. request_fulluri
    1. Type: boolean
    2. When set to TRUE, the entire URI will be used when constructing(建構) the request. (i.e. GET http://www.example.com/path/to/file.html HTTP/1.0). While this is a non-standard request format, some proxy servers require it.
    3. Defaults to FALSE.
  7. follow_location integer
    1. Type: integer
    2. Follow Location header redirects. Set to 0 to disable
    3. Defaults to 1
  8. max_redirects
    1. Type: integer
    2. The max number of redirects to follow. Value 1 or less means that no redirects are followed.
    3. Defaults to 20.
  9. protocol_version
    1. Type: float
    2. HTTP protocol version.
    3. Defaults to 1.0.
    4. Note: PHP prior to 5.3.0 does not implement chunked transfer decoding. If this value is set to 1.1 it is your responsibility(回應) to be 1.1 compliant(兼容)
  10. timeout
    1. Type: float
    2. Read timeout in seconds, specified by a float (e.g. 10.5).
  11. ignore_errors
    1. Type: boolean
    2. Fetch the content even on failure status codes.
    3. Defaults to FALSE.

Examples: Fetch a page and send POST data

<?php

$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

?>

Examples: Ignore redirects but fetch headers and content

$url = "http://www.example.org/header.php";
$opts = array(
    'http' => array(
        'method' => 'GET',
        'max_redirects' => '0',
        'ignore_errors' => '1'
    )
);
$context = stream_context_create($opts);
$stream = fopen($url, 'r'. false, $context);
var_dump(stream_get_meta_data($stream));
var_dump(stream_get_contents($stream));

FTP context options

FTP context options — FTP context option listing

Options
  1. overwrite
    1. type: boolean
    2. content: Allow overwriting of already existing files on remote server. Applies to write mode (uploading) only.
    3. Defaults: FALSE
  2. resume_pos
    1. type: integer
    2. content: File offset at which to begin transfer. Applies to read mode (downloading) only.
    3. Defaults: 0 (Beginning of File)
  3. proxy
    1. type: string
    2. content: Proxy FTP request via http proxy server. Applies to file read operations only. Ex: tcp://squid.example.com:8000.

SSL context options

Options
  1. peer_name
    1. type: string
    2. content: Peer name to be used. If this value is not set, then the name is guessed based on the hostname used when opening the stream.
  2. verify_peer
    1. type: boolean
    2. content: Require verification of SSL certificate used.
    3. Default: TRUE
  3. verify_peer_name
    1. type: boolean
    2. content: Require verification of peer name
    3. Default: TRUE
  4. allow_self_signed
    1. type: boolean
    2. content: Allow self-signed certificates. Requires verify_peer.
    3. Default: FALSE
  5. cafile
    1. type: string
    2. content: If cafile is not specified or if the certificate is not found there, the directory pointed to by capath is searched for a suitable certificate. capath must be a correctly hashed certificate directory.
  6. local_cert
    1. type: string
    2. content: Path to local certificate file on filesystem. It must be a PEM encoded file which contains your certificate and private key. It can optionally contain the certificate chain of issuers. The private key also may be contained in a separate file specified by local_pk.
  7. local_pk
    1. type: string
    2. content: Path to local private key file on filesystem in case of separate files for certificate (local_cert) and private key.
  8. passphrase(密碼)
    1. type: string
    2. Passphrase with which your local_cert file was encoded.
  9. CN_match
    1. type: string
    2. content: Common Name we are expecting. PHP will perform(執行) limited wildcard(通配符) matching. If the Common Name does not match this, the connection attempt will fail.
  10. verify_depth
    1. type: integer
    2. content: Abort(丟棄) if the certificate chain is too deep.
    3. Default: no verification.
  11. ciphers(密碼)
    1. type: string
    2. content: Sets the list of available ciphers. The format of the string is described in » ciphers(1).
    3. Default: DEFAULT
  12. capture_peer_cert
    1. type: boolean
    2. content: If set to TRUE a peer_certificate context option will be created containing the peer certificate.
  13. capture_peer_cert_chain
    1. type: boolean
    2. content: If set to TRUE a peer_certificate_chain context option will be created containing the certificate chain.
  14. SNI_enabled
    1. type: boolean
    2. content: If set to TRUE server name indication will be enabled. Enabling SNI allows multiple certificates on the same IP address.
  15. SNI_server_name
    1. type: string
    2. content: If set, then this value will be used as server name for server name indication. If this value is not set, then the server name is guessed based on the hostname used when opening the stream.
    3. Note: This option is deprecated, in favour of peer_name, as of PHP 5.6.0.
  16. disable_compression(壓縮)
    1. type: boolean
    2. content: If set, disable TLS compression. This can help mitigate(減輕) the CRIME attack vector.
  17. peer_fingerprint(指紋)
    1. type: string | array
      1. string: determine which hashing algorithm is applied, either "md5" (32) or "sha1" (40).
      2. array: When an array is used, the keys indicate the hashing algorithm name and each corresponding value is the expected digest.
    2. content: Aborts when the remote certificate digest(文摘) doesn't match the specified hash.

CURL context options

Options
  1. method:
    1. type: string
    2. content: GET, POST, or any other HTTP method supported by the remote server.
    3. Default: GET
  2. header
    1. type: string
    2. content: Additional headers to be sent during request. Values in this option will override other values (such as User-agent:, Host:, and Authentication)
  3. user_agent
    1. type: string
    2. content: Value to send with User-Agent: header.
    3. Default: the user_agent php.ini setting is used.
  4. content
    1. type: string
    2. content: Additional data to be sent after the headers. This option is not used for GET or HEAD requests.
  5. proxy
    1. type: string
    2. content: URI specifying address of proxy server. (e.g. tcp://proxy.example.com:5100).
  6. max_redirects
    1. type: integer
    2. content: The max number of redirects to follow. Value 1 or less means that no redirects are followed.
    3. Default: 20
  7. curl_verify_ssl_host
    1. type: boolean
    2. content: Verify the host.
    3. Defaults: FALSE
    4. Note: This option is available for both the http and ftp protocol wrappers.
  8. curl_verify_ssl_peer
    1. type: boolean
    2. content: Require verification of SSL certificate used
    3. Defaults: FALSE
    4. Note: This option is available for both the http and ftp protocol wrappers.

Phar context options

Options
  1. compress:
    1. type: int
    2. content: One of Phar compression constants
  2. metadata:
    1. type: mixed
    2. content: Phar metadata. See Phar::setMetadata().

MongoDB context options

Options
  1. log_cmd_insert
    1. type: callable
    2. content: A callback function called when inserting a document
  2. log_cmd_delete
    1. type: callable
    2. content: A callback function called when deleting a document
  3. log_cmd_update
    1. type: callable
    2. content: A callback function called when updating a document
  4. log_write_batch
    1. type: callable
    2. content: A callback function called when executing a Write Batch
  5. log_reply
    1. type: callable
    2. content: A callback function called when reading a reply from MongoDB
  6. log_getmore
    1. type: callable
    2. content: A callback function called when retrieving more results from a MongoDB cursor.
  7. log_killcursor
    1. type: callable
    2. content: A callback function called executing a killcursor opcode(操作碼)

results matching ""

    No results matching ""